﻿using ManagementApi.Models;
using ManagementApi.Models.CheckIn;
using ManagementApi.Models.Manage.CheckIn;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using ManagementApi.Mvc.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules;
using Modules.CheckIn;
using Modules.Reports;
using PmSoft.Exceptions;

namespace ManagementApi.Controllers.ConstructorPannel
{
    /// <summary>
    /// 建设单位-考勤统计
    /// </summary>
    [Route("/api/v1/constructor/checkindetail")]
    [ApiController]
    [Authorize(Roles = "Constructor")]
    [TypeFilter(typeof(RequireSelProjectActionFilter))]
    public class CheckInDetailController : ControllerBase
    {
        private readonly CheckInDetailService checkInDetailService;
        private readonly ConstructorApplicationContext applicationContext;
        private readonly ReportProjectGroupDaysService reportProjectGroupDaysService;

        /// <summary>
        /// </summary>
        /// <param name="checkInDetailService"></param>
        /// <param name="applicationContext"></param>
        public CheckInDetailController(CheckInDetailService checkInDetailService,
            ConstructorApplicationContext applicationContext,
            ReportProjectGroupDaysService reportProjectGroupDaysService)
        {
            this.checkInDetailService = checkInDetailService;
            this.applicationContext = applicationContext;
            this.reportProjectGroupDaysService = reportProjectGroupDaysService;
        }

        /// <summary>
        /// 获取考勤记录列表(分页)
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<CheckInDetailDto>> GetCheckInDetail([FromQuery] CheckInDetailQuery query, int pageIndex = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            var data = checkInDetailService.GetCheckInDetailMonths(query, pageIndex, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsCheckInDetailDto(applicationContext, applicationContext.CurrentProjectInfo); });
            return ApiResult<PageDto<CheckInDetailDto>>.Success(dtos);
        }

        #region 人员考勤统计

        /// <summary>
        /// 获取按天统计考勤数据
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page_project_day")]
        public ApiResult<PageDto<ReportProjectGroupDays>> GetReportProjectGroupDaysList([FromQuery] ReportProjectGroupDaysQuery query, int pageIndex = 1, int pageSize = 2)
        {
            var data = reportProjectGroupDaysService.GetReportProjectDays(query, pageIndex, pageSize);
            return ApiResult<PageDto<ReportProjectGroupDays>>.Success(data.AsPageDto(source => { return source; }));
        }

        /// <summary>
        /// 人员考勤月统计
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("page_person_month")]
        public ApiResult<PageDto<CheckInDetailPersonDto>> GetCheckInDetailPerson([FromQuery] CheckInDetailMonthQuery query, int pageNum = 1, int pageSize = 20)
        {
            if (!query.Month.HasValue)
                return ApiResult<PageDto<CheckInDetailPersonDto>>.Failure("请选择一个月份查询");
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            var data = checkInDetailService.GetCheckInDetailsByPersonGroup(query, pageNum, pageSize);
            var dtos = data.AsPageDto(source => { return source.AsDto(applicationContext, applicationContext.CurrentProjectInfo); });
            return ApiResult<PageDto<CheckInDetailPersonDto>>.Success(dtos);
        }

        /// <summary>
        /// 查询当前人员信息统计某个月考情详情
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        [HttpGet("getCheckInDetailPersonForm/{memberId}")]
        public ApiResult<IEnumerable<int>> GetCheckInDetailPersonForm(int memberId, int month)
        {
            var data = checkInDetailService.GetCheckInDetailPersonForm(memberId, month);
            return ApiResult<IEnumerable<int>>.Success(data);
        }

        #endregion 人员考勤统计
    }
}